package cn.billy.pet.service.impl;

import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.utils.*;
import cn.billy.order.domain.OrderPetAcquisition;
import cn.billy.order.mapper.OrderPetAcquisitionMapper;
import cn.billy.org.domain.Shop;
import cn.billy.org.mapper.ShopMapper;
import cn.billy.pet.domain.Pet;
import cn.billy.pet.domain.SearchMasterMsg;
import cn.billy.pet.domain.SearchMasterMsgAuditLog;
import cn.billy.pet.dto.SearchMasterMsgDto;
import cn.billy.pet.mapper.PetMapper;
import cn.billy.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.billy.pet.mapper.SearchMasterMsgMapper;
import cn.billy.pet.service.ISearchMasterMsgService;
import cn.billy.basic.service.impl.BaseServiceImpl;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author billy
 * @since 2022-07-19
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg) {
        String title = searchMasterMsg.getTitle();
        Integer age = searchMasterMsg.getAge();
        BigDecimal price = searchMasterMsg.getPrice();
        String address = searchMasterMsg.getAddress();
        String name = searchMasterMsg.getName();
        String note = searchMasterMsg.getNote();
        String coatColor = searchMasterMsg.getCoatColor();
        if (StringUtils.isEmpty(title)
                || StringUtils.isEmpty(price)
                || StringUtils.isEmpty(address)){
            throw new BusinessException("参数不能为空");
        } else {
            searchMasterMsgMapper.add(searchMasterMsg);
        }
        StringBuffer buffer = new StringBuffer()
                .append(note)
                .append(address)
                .append(name)
                .append(coatColor);
        if (!BaiduAuditUtils.TextCensor(buffer.toString())){
            searchMasterMsg.setState(-1);
            SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
            searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
            searchMasterMsgAuditLog.setAuditTime(new Date());
            searchMasterMsgAuditLog.setState(0);
            searchMasterMsgAuditLog.setNote("填写的信息不合法，可能是：名字，地址，毛色等");
            searchMasterMsgAuditLogMapper.add(searchMasterMsgAuditLog);
        } else {
            searchMasterMsg.setState(1);
            Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
            List<Shop> shops = shopMapper.loadAll();
            System.out.println(shops);
            Shop shop = DistanceUtil.getNearestShop(point, shops);
            if (shop != null){
                searchMasterMsg.setShopId(shop.getId());
                SendMsg_webchinese.sendMsg(shop.getTel(),"亲，您有新的订单，请及时处理");
            }
        }
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    @Override
    public void accept(SearchMasterMsgDto dto) {
        if (dto.getMsgId() == null || dto.getHandler() == null || dto.getNote() == null){
            throw new BusinessException("参数不能为空");
        }
        // 1.查询此寻主消息是否已经被接单，即便被接单，是否已经是取消状态，也就是此寻主消息的收购订单是否存在，如果存在是否是取消状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.getById(dto.getMsgId());
        if (orderPetAcquisition != null){
            throw new BusinessException("订单也被抢单");
        }
        // 2.生成宠物基本信息，详情数据可以等店铺上架时，再进行生成
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.getById(dto.getMsgId());
        Pet pet = msg2Pet(searchMasterMsg);
        petMapper.add(pet);

        // 3.生成收购单信息
        orderPetAcquisition = initOrderPetAcquisition(dto, searchMasterMsg, pet);
        orderPetAcquisitionMapper.add(orderPetAcquisition);

    }

    @Override
    public void reject(Long msgId) {
        // 1.将寻主消息所绑定的店铺Id设置为null，防止已经被某个员工接单，那么将接单用户也置为null
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.getById(msgId);
        searchMasterMsg.setShopId(null);
        searchMasterMsg.setUserId(null);
        searchMasterMsgMapper.update(searchMasterMsg);

    }

    /*/* 
    * @Title: initOrderPetAcquisition
    * @Description: 初始化收购单对象
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/7/20 18:32  
    * @Parameters: [dto, searchMasterMsg, pet]
    * @Return cn.billy.order.domain.OrderPetAcquisition
    */
    private OrderPetAcquisition initOrderPetAcquisition(SearchMasterMsgDto dto, SearchMasterMsg searchMasterMsg, Pet pet) {
        OrderPetAcquisition orderPetAcquisition = new OrderPetAcquisition();
        orderPetAcquisition.setOrderSn(CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUserId()));
        orderPetAcquisition.setDigest(dto.getHandler() + "接单了-"+ pet.getName());
        orderPetAcquisition.setLastcomfirmtime(new Date(System.currentTimeMillis() + (2 * 24 * 60 * 60 * 1000)));
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisition.setUserId(searchMasterMsg.getUserId());
        orderPetAcquisition.setShopId(searchMasterMsg.getShopId());
        orderPetAcquisition.setAddress(searchMasterMsg.getAddress());
        orderPetAcquisition.setEmpId(dto.getHandler());
        orderPetAcquisition.setSearchMasterMsgId(searchMasterMsg.getId());
        return orderPetAcquisition;
    }

    /*/*
    * @Title: msg2Pet
    * @Description: 初始化宠物基本信息对象
    * @Author: Billy
    * @Version: 1.0
    * @Date:  2022/7/20 18:33
    * @Parameters: [searchMasterMsg]
    * @Return cn.billy.pet.domain.Pet
    */
    private Pet msg2Pet(SearchMasterMsg searchMasterMsg){
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setUserId(searchMasterMsg.getUserId());
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        return pet;
    }


}
